#include "calculate_lowercomputer_data.h"
#include "global_variant.h"

#include <cmath>
#include <QDebug>

// CalculateLowerComputerData::CalculateLowerComputerData(QObject *parent)
//     : QObject{parent}
CalculateLowerComputerData::CalculateLowerComputerData()
{
    //启动定时器：用于发送心跳
    m_timer = new QTimer(this);

    //连接定时器的信号与槽，定时时间一到就会发送 timeout() 信号，从而触发槽函数dealHeartBeat（处理心跳）
    connect(m_timer, &QTimer::timeout,
            this, [&](){
/**
大闸制动，小闸缓解，制动压力不变
小闸制动，只有大闸的下压可以减少制动压力
紧急制动，只有紧急制动缓
辅助制动，只有辅助制动可以缓解
总风缸压力降到660一下，会自动冲压到720
**/
        //qDebug()<<"CalculateLowerComputerData";
        if(g_startDieselEngine == true && m_totalWindCylinderPressure < 720.0 && m_isImpulsetotalWindCylinderPressure){//开启发动机
            //只冲总风缸压力，压力不足660时，再重新冲到720
            //列车管、均风缸、制动缸的压力，均不能大于总风杠
            if(m_totalWindCylinderPressure + m_totalWindCylinderPressureAcceleration_1 + g_throttleHandle * m_totalWindCylinderPressureAcceleration_1 < 720.0){
                m_totalWindCylinderPressure += m_totalWindCylinderPressureAcceleration_1 + g_throttleHandle * m_totalWindCylinderPressureAcceleration_1;
            }else{
                m_totalWindCylinderPressure = 720.0;
                m_isImpulsetotalWindCylinderPressure = false;
            }
            g_totalWindCylinderPressure = round(m_totalWindCylinderPressure);
        }

        if(g_startDieselEngine == false){
            // qDebug()<<"g_startDieselEngine == false";

            // qDebug()<<"m_totalWindCylinderPressureAcceleration_1:"<<m_totalWindCylinderPressureAcceleration_1;
            // qDebug()<<"g_throttleHandle * m_totalWindCylinderPressureAcceleration_1:"<<g_throttleHandle * m_totalWindCylinderPressureAcceleration_1;

            // qDebug()<<"m_totalWindCylinderPressure:"<<m_totalWindCylinderPressure;
            // qDebug()<<"m_totalWindCylinderPressureAcceleration_1 - g_throttleHandle * m_totalWindCylinderPressureAcceleration_1:"<<m_totalWindCylinderPressureAcceleration_1 - g_throttleHandle * m_totalWindCylinderPressureAcceleration_1;


            if(m_totalWindCylinderPressure - m_totalWindCylinderPressureAcceleration_1 - g_throttleHandle * m_totalWindCylinderPressureAcceleration_1 > 0.0){
                m_totalWindCylinderPressure = m_totalWindCylinderPressure - m_totalWindCylinderPressureAcceleration_1 - g_throttleHandle * m_totalWindCylinderPressureAcceleration_1;
            }else{
                m_totalWindCylinderPressure = 0.0;
            }
            g_totalWindCylinderPressure = round(m_totalWindCylinderPressure);

           // qDebug()<<"m_totalWindCylinderPressure:"<<m_totalWindCylinderPressure;
        }

        //大闸保压位/运转位
        if(g_bigGateType == BigSmallGate::PreservationPressureLocationType
            || g_bigGateType == BigSmallGate::OperatingPositionType
            || g_smallGateType == BigSmallGate::PreservationPressureLocationType
            || g_smallGateType == BigSmallGate::OperatingPositionType){
            if(m_brakeCylinderPressure - m_brakeCylinderPressureAcceleration_9 > 0.0){
                m_brakeCylinderPressure -= m_brakeCylinderPressureAcceleration_9;
            }else{
                m_brakeCylinderPressure = 0.0;
            }
            g_brakeCylinderPressure = round(m_brakeCylinderPressure);

            if(m_trainPipePressure - m_trainPipePressureAcceleration_9 > 0.0){
                m_trainPipePressure -= m_trainPipePressureAcceleration_9;
            }else{
                m_trainPipePressure = 0.0;
            }
            g_trainPipePressure = round(m_trainPipePressure);
        }

        if(g_emergencyBraking){//紧急制动
            m_maxBrakeCylinderPressure = 430.0;

            if(m_trainPipePressure - m_trainPipePressureAcceleration_6 > 0.0){//够减才减
                m_trainPipePressure -= m_trainPipePressureAcceleration_6;
            }else{
                m_trainPipePressure = 0.0;
            }
            g_trainPipePressure = round(m_trainPipePressure);

            if(m_brakeCylinderPressure + m_brakeCylinderPressureAcceleration_6 < 400.0 && m_totalWindCylinderPressure - m_brakeCylinderPressureAcceleration_6/5 > m_brakeCylinderPressure){
                m_totalWindCylinderPressure -= m_brakeCylinderPressureAcceleration_6/5;
            }

            if(m_brakeCylinderPressure + m_brakeCylinderPressureAcceleration_6 < 400.0){
                m_brakeCylinderPressure += m_brakeCylinderPressureAcceleration_6;
            }else if(m_brakeCylinderPressure + m_brakeCylinderPressureAcceleration_6 < 430.0){
                m_brakeCylinderPressure += m_brakeCylinderPressureAcceleration_8;
            }else{
                m_brakeCylinderPressure = 430.0;
            }

            if(m_brakeCylinderPressure > m_totalWindCylinderPressure){
                m_brakeCylinderPressure = m_totalWindCylinderPressure;
            }

            g_brakeCylinderPressure = round(m_brakeCylinderPressure);
        }else{
            //辅助制动
            if(g_auxiliaryBraking == true){
                if(m_brakeCylinderPressure + m_brakeCylinderPressureAcceleration_7  < 360.0 && m_totalWindCylinderPressure - m_brakeCylinderPressureAcceleration_7/5 > m_brakeCylinderPressure){
                    m_totalWindCylinderPressure -= m_brakeCylinderPressureAcceleration_7/5;
                }

                if(m_brakeCylinderPressure + m_brakeCylinderPressureAcceleration_7  < 360.0 && m_brakeCylinderPressure  <= m_totalWindCylinderPressure){
                    m_brakeCylinderPressure += m_brakeCylinderPressureAcceleration_7;
                }else{
                    m_brakeCylinderPressure = 360.0;
                }

                if(m_brakeCylinderPressure >m_totalWindCylinderPressure){
                    m_brakeCylinderPressure = m_totalWindCylinderPressure;
                }else{

                }

                g_brakeCylinderPressure = round(m_brakeCylinderPressure);
            }else{
                //大闸
                if(g_isSmallGateTypeBrakeCylinder == false){ //小闸制动，只有大闸下压才降制动压力
                    if(g_bigGateType == BigSmallGate::ReliefPositionType){//缓解位
                        if( m_trainPipePressure + m_trainPipePressureAcceleration_10 < 500.0){
                            m_trainPipePressure += m_trainPipePressureAcceleration_10;

                            m_totalWindCylinderPressure -= m_trainPipePressureAcceleration_10/50;
                        }else{
                            m_trainPipePressure = 500.0;
                        }

                        if(m_trainPipePressure > m_totalWindCylinderPressure){
                            m_trainPipePressure = m_totalWindCylinderPressure;
                        }else{

                        }
                        g_trainPipePressure = round(m_trainPipePressure);

                        if( m_equalizeCylinderPressure + m_equalizeCylinderPressureAcceleration_10 < 250.0){
                            m_equalizeCylinderPressure += m_equalizeCylinderPressureAcceleration_10;

                            m_totalWindCylinderPressure -= m_equalizeCylinderPressureAcceleration_10/50;
                        }else if( m_equalizeCylinderPressure + m_equalizeCylinderPressureAcceleration_11 < 500.0){
                            m_equalizeCylinderPressure += m_equalizeCylinderPressureAcceleration_11;

                            m_totalWindCylinderPressure -= m_equalizeCylinderPressureAcceleration_11/50;
                        }
                        else{
                            m_equalizeCylinderPressure = 500.0;
                        }

                        if(m_equalizeCylinderPressure > m_totalWindCylinderPressure){
                            m_equalizeCylinderPressure = m_totalWindCylinderPressure;
                        }else{

                        }

                        g_equalizeCylinderPressure = round(m_equalizeCylinderPressure);

                        if(m_brakeCylinderPressure - m_brakeCylinderPressureAcceleration_2 > 0.0){
                            m_brakeCylinderPressure -= m_brakeCylinderPressureAcceleration_2;
                        }else{
                            m_brakeCylinderPressure = 0.0;
                            g_isBigGateTypeBrakeCylinder = false;
                            g_isEmergencyBraking = false;
                        }
                        g_brakeCylinderPressure = round(m_brakeCylinderPressure);

                        if(m_brakeCylinderPressure <= 360.0){
                            m_maxBrakeCylinderPressure = 360.0;
                        }
                    }
                }


                if(g_bigGateType == BigSmallGate::PressDownType){//下压位(大闸)
                    if(m_brakeCylinderPressure - m_brakeCylinderPressureAcceleration_4  > 0.0){
                        m_brakeCylinderPressure -= m_brakeCylinderPressureAcceleration_4;
                    }else{
                        m_brakeCylinderPressure = 0.0;
                        g_isBigGateTypeBrakeCylinder = false;
                        g_isSmallGateTypeBrakeCylinder = false;
                    }
                    g_brakeCylinderPressure = round(m_brakeCylinderPressure);
                }

                //小闸
                if(g_isBigGateTypeBrakeCylinder == false){//小闸不能缓解大闸的制动
                    if(g_smallGateType == BigSmallGate::PressDownType
                        || g_smallGateType == BigSmallGate::ReliefPositionType){//下压位、缓解位（小闸）
                        if( m_brakeCylinderPressure - m_brakeCylinderPressureAcceleration_5 > 0.0){
                            m_brakeCylinderPressure -= m_brakeCylinderPressureAcceleration_5;
                        }else{
                            m_brakeCylinderPressure = 0.0;
                            g_isSmallGateTypeBrakeCylinder = false;
                        }
                        g_brakeCylinderPressure = round(m_brakeCylinderPressure);
                    }
                }

                //大闸
                if(g_bigGateType == BigSmallGate::RetarderLocationType){//制动位
                    if(m_trainPipePressure - m_trainPipePressureAcceleration_2 > 0.0){
                        m_trainPipePressure -= m_trainPipePressureAcceleration_2;
                    }else{
                        m_trainPipePressure = 0.0;
                    }
                    g_trainPipePressure = round(m_trainPipePressure);

                    if(m_equalizeCylinderPressure - m_equalizeCylinderPressureAcceleration_2 > 0.0){
                        m_equalizeCylinderPressure -= m_equalizeCylinderPressureAcceleration_2;

                    }else{
                        m_equalizeCylinderPressure = 0.0;
                    }
                    g_equalizeCylinderPressure = round(m_equalizeCylinderPressure);

                    if(m_brakeCylinderPressure + m_brakeCylinderPressureAcceleration_3 < 360.0){
                        m_brakeCylinderPressure += m_brakeCylinderPressureAcceleration_3;

                        m_totalWindCylinderPressure -= m_brakeCylinderPressureAcceleration_3/200;
                    }else{
                        //紧急制动

                        m_brakeCylinderPressure = m_maxBrakeCylinderPressure;
                    }

                    if(m_brakeCylinderPressure > m_totalWindCylinderPressure){
                        m_brakeCylinderPressure = m_totalWindCylinderPressure;
                    }else{

                    }

                    g_brakeCylinderPressure = round(m_brakeCylinderPressure);
                }

                //小闸制动
                if(g_smallGateType == BigSmallGate::RetarderLocationType){
                    if(m_brakeCylinderPressure + m_brakeCylinderPressureAcceleration_10  < 360.0){
                        m_brakeCylinderPressure += m_brakeCylinderPressureAcceleration_10;
                    }else{
                        m_brakeCylinderPressure = m_maxBrakeCylinderPressure;
                    }

                    if(m_brakeCylinderPressure >m_totalWindCylinderPressure){
                        m_brakeCylinderPressure = m_totalWindCylinderPressure;
                    }

                    g_brakeCylinderPressure = round(m_brakeCylinderPressure);
                }
            }
        }

        if(m_totalWindCylinderPressure < 0.0){
            m_totalWindCylinderPressure = 0.0;
        }
        g_totalWindCylinderPressure = round(m_totalWindCylinderPressure);

        //总风降到660以下，将会自动冲压
        if(m_totalWindCylinderPressure < 660.0){
            m_isImpulsetotalWindCylinderPressure = true;
        }

        //刷新界面
        emit refreshShow();
    });

}

void CalculateLowerComputerData::stopCalculateWork()
{
    m_timer->stop();
}

void CalculateLowerComputerData::startCalculateWork()
{

    m_timer->start(m_refreshTime);//每20ms刷新一次
}


